package com.solairen.myapplication.Pager

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.solairen.myapplication.R
import com.solairen.myapplication.data.DiaData
import com.solairen.myapplication.data.RESULTS_KEY
import com.solairen.myapplication.data.SP_FILENAME
import com.solairen.myapplication.databinding.ItemDiagnoseResultBinding
import com.solairen.myapplication.databinding.PersonnelDiagnosisRecordBinding
import com.solairen.myapplication.utils.gone
import com.solairen.myapplication.utils.toJsonString
import com.solairen.myapplication.utils.toList
import com.solairen.myapplication.utils.visible
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream

/**
 *
 *
 * @Date 2024/3/9 21:47
 */
class DiagnosisRecordActivity : AppCompatActivity() {

    private lateinit var viewBinding: PersonnelDiagnosisRecordBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = PersonnelDiagnosisRecordBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        init()
    }

    private fun init() {
        viewBinding.historyIvBack.setOnClickListener {
            finish()
        }

        viewBinding.recyclerView.also {
            it.layoutManager = LinearLayoutManager(this)
            it.adapter = MAdapter(createItem())
        }
    }

    /**
     * fixme 从历史结果中获取数据
     */
    private fun createItem() : List<DiaData> {
        val sp = getSharedPreferences(SP_FILENAME, Context.MODE_PRIVATE)
        sp.getString(RESULTS_KEY, null)?.also {
            return it.toList(DiaData::class.java)
        }
        return emptyList()
    }

//    private fun createItem() : List<DiaData> {
//        var inputStream: InputStream? = null
//        try {
//            // 获取 AssetManager 对象
//            val assetManager = assets
//            // 打开文件并读取数据
//            inputStream = assetManager.open("diagnose.json")
//            // 将 InputStream 转换为 JSON 字符串
//            val jsonString = inputStream.bufferedReader().use(BufferedReader::readText)
//            return jsonString.toList(DiaData::class.java)
//        } catch (e: IOException) {
//            e.printStackTrace()
//        } finally {
//            try {
//                inputStream?.close()
//            } catch (e: IOException) {
//                e.printStackTrace()
//            }
//        }
//        return emptyList()
//    }

    internal class MAdapter(private var items: List<DiaData>) :
        RecyclerView.Adapter<VH>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
            val view: View =
                LayoutInflater.from(parent.context).inflate(R.layout.item_diagnose_result, null)
            return VH(view)
        }

        override fun onBindViewHolder(holder: VH, position: Int) {
            holder.bind(items[position], position)
        }

        override fun getItemCount(): Int {
            return items.size
        }
    }

    internal class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private lateinit var itemVB: ItemDiagnoseResultBinding

        init {
            itemVB = ItemDiagnoseResultBinding.bind(itemView)
        }

        fun bind(item: DiaData, pos: Int) {
            if (pos == 0) {
                gone(itemVB.space)
            } else {
                visible(itemVB.space)
            }
            itemVB.symptomContent.text = item.symptom
//            itemVB.degreeContent.text = item.rank
        }
    }
}